package com.heima;


import com.baidu.aip.util.Base64Util;
import com.heima.utils.baidu.FileUtil;
import com.heima.utils.baidu.HttpUtil;
import okhttp3.*;
import org.junit.Test;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.IOException;
import java.net.URLEncoder;
/**
 * @author yangkexuan
 * @className AuthText
 * @date 2023/8/3 9:46
 * @description
 */
@SpringBootTest
public class AuthText {

        // 获取AK
        static final OkHttpClient HTTP_CLIENT = new OkHttpClient().newBuilder().build();
        final String client_id = "n9rxmkA3qH2nSlAfYOM4O9ew";
        final String client_secret = "G5v26Y1jBqz7K0KoXxEfPQFfOYCFVEBB";

        public void Aktest() throws IOException {
            MediaType mediaType = MediaType.parse("application/json");
            RequestBody body = RequestBody.create(mediaType, "");
            Request request = new Request.Builder()
                    .url("https://aip.baidubce.com/oauth/2.0/token?client_id="+client_id+"&client_secret="+client_secret+"&grant_type=client_credentials")
                    .method("POST", body)
                    .addHeader("Content-Type", "application/json")
                    .addHeader("Accept", "application/json")
                    .build();
            Response response = HTTP_CLIENT.newCall(request).execute();
            System.out.println(response.body().string());
        }

        public static String idcard() {
            // 请求url
            String url = "https://aip.baidubce.com/rest/2.0/ocr/v1/idcard";
            try {
                // 本地文件路径
                String filePath = "E:\\图片\\身份证正面测试.jpg";
                byte[] imgData = FileUtil.readFileByBytes(filePath);
                String imgStr = Base64Util.encode(imgData);
                String imgParam = URLEncoder.encode(imgStr, "UTF-8");

                String param = "id_card_side=" + "front" + "&image=" + imgParam;

                // 注意这里仅为了简化编码每一次请求都去获取access_token，线上环境access_token有过期时间， 客户端可自行缓存，过期后重新获取。
                String accessToken = "24.1e3f878bc5f9536d9fa48b7509558c9c.2592000.1693618447.282335-37069070";

                String result = HttpUtil.post(url, accessToken, param);
                System.out.println(result);

                return result;
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }

    /**
     * {"words_result":{"姓名":{"location":{"top":183,"left":290,"width":95,"height":56},
     * "words":"常涛"},"民族":{"location":{"top":280,"left":510,"width":39,"height":47},"words":"汉"},
     * "住址":{"location":{"top":460,"left":278,"width":472,"height":110},"words":"河北省邯郸市肥乡县肥乡镇"},
     * "公民身份号码":{"location":{"top":687,"left":458,"width":626,"height":59},"words":"411526199706013217"},
     * "出生":{"location":{"top":369,"left":275,"width":377,"height":46},"words":"19970601"},
     * "性别":{"location":{"top":280,"left":288,"width":41,"height":50},"words":"男"}},
     * "words_result_num":6,"image_status":"normal","idcard_number_type":1,"log_id":1686919890661724277}
     */

    @Test
        public void idcardtest() {
            AuthText.idcard();
        }
}
